import { verifyToken, verifyAdmin } from "../middleware/authJwt.js";
import db from "../models/index.js";

const userRoutes = (app) => {
  app.get("/user/getCommonUserContent", [verifyToken], async (req, res) => {
    res.status(200).json({ commonUserContent: 1111 });
  });

  app.get(
    "/user/getAdminContent",
    [verifyToken, verifyAdmin],
    async (req, res) => {
      res.status(200).json({ adminContent: [1, 2, 3] });
    }
  );
  app.get(
    "/user/isLogin",
    [verifyToken],
    async (req, res) => {
      const userId = req.userId;
      const q = "select * from users where id = ?";
      db.query(q, [userId], (err, data) => {
      if (err) return res.status(500).json(err);
      if (!data.length) return res.status(404).json("User not found");
      const {password, ...user} = data[0];
      res.status(200).json({...user, message: 'token is valid'})
    });
  }
  )
};

export default userRoutes;
